<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="categorizeId">
        <el-select size="mini" v-model="queryParams.categorizeId" @change="getList">
          <el-option
            @click.native="changeCategorize(item)"
            v-for="item in options"
            :key="item.id"
            :label="item.zhCategorizeName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="title">
        <el-input
          size="mini"
          v-model="queryParams.title"
          placeholder="请输入问题关键字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select size="mini" v-model="queryParams.status" placeholder="数据状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:help:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="标题(中文)" align="center" prop="zhTitle" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="标题(英文)" align="center" prop="enTitle" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="标题(日文)" align="center" prop="jaTitle" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="内容(中文)" align="center" prop="zhContent" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="内容(英文)" align="center" prop="enContent" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="内容(日文)" align="center" prop="jaContent" width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:help:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:help:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="分类名称(中文)">
          <el-input v-model="form.zhCategorizeName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="分类名称(英文)">
          <el-input v-model="form.enCategorizeName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="分类名称(日文)">
          <el-input v-model="form.jaCategorizeName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="标题(中文)" prop="zhTitle">
          <el-input v-model="form.zhTitle" placeholder="请输入标题(中文)" />
        </el-form-item>
        <el-form-item label="标题(英文)" prop="enTitle">
          <el-input v-model="form.enTitle" placeholder="请输入标题(英文)" />
        </el-form-item>
        <el-form-item label="标题(日文)" prop="jaTitle">
          <el-input v-model="form.jaTitle" placeholder="请输入标题(日文)" />
        </el-form-item>
        <el-form-item label="内容(中文)" prop="zhContent">
          <el-input type="textarea" v-model="form.zhContent" placeholder="请输入内容(中文)" />
        </el-form-item>
        <el-form-item label="内容(英文)" prop="enContent">
          <el-input type="textarea" v-model="form.enContent" placeholder="请输入内容(英文)" />
        </el-form-item>
        <el-form-item label="内容(日文)" prop="jaContent">
          <el-input type="textarea" v-model="form.jaContent" placeholder="请输入内容(日文)" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategorize,optionselect ,delData,updateData, addData,getData, listData } from "@/api/triones/help";

export default {
  name: "data",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      defaultCategorizeId: "",
      zhDefaultCategorizeName: "",
      enDefaultCategorizeName: "",
      jaDefaultCategorizeName: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        categorizeId: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zhTitle: [
          { required: true, message: "标题(中文)不能为空", trigger: "blur" }
        ],
        enTitle: [
          { required: true, message: "标题(英文)不能为空", trigger: "blur" }
        ],
        jaTitle: [
          { required: true, message: "标题(日文)不能为空", trigger: "blur" }
        ],
        zhContent: [
          { required: true, message: "内容(中文)不能为空", trigger: "blur" }
        ],
        enContent: [
          { required: true, message: "内容(英文)不能为空", trigger: "blur" }
        ],
        jaContent: [
          { required: true, message: "内容(日文)不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const categorizeId = this.$route.params && this.$route.params.categorizeId;
    this.getCategorize(categorizeId);
    this.getCategorizeList();
  },
  methods: {
    /** 切换函数 */
    changeCategorize(item) {
      this.queryParams.categorizeId = item.id;
      this.defaultCategorizeId = item.id;
      this.zhDefaultCategorizeName = item.zhCategorizeName;
      this.enDefaultCategorizeName = item.enCategorizeName;
      this.jaDefaultCategorizeName = item.jaCategorizeName;
    },
    /** 查询字典类型详细 */
    getCategorize(categorizeId) {
      getCategorize(categorizeId).then(response => {
        this.queryParams.categorizeId = response.data.id;
        this.defaultCategorizeId = response.data.id;
        this.zhDefaultCategorizeName = response.data.zhCategorizeName;
        this.enDefaultCategorizeName = response.data.enCategorizeName;
        this.jaDefaultCategorizeName = response.data.jaCategorizeName;
        this.getList();
      });
    },
    /** 查询字典类型列表 */
    getCategorizeList() {
      optionselect().then(response => {
        this.options = response.data;
      });
    },
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        categorizeId: this.defaultCategorizeId,
        zhTitle: undefined,
        enTitle: undefined,
        jaTitle: undefined,
        zhContent: undefined,
        enContent: undefined,
        jaContent: undefined,
        sort: 0,
        status: "0",
        remark: undefined,
        zhCategorizeName: this.zhDefaultCategorizeName,
        enCategorizeName: this.enDefaultCategorizeName,
        jaCategorizeName: this.jaDefaultCategorizeName
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.categorizeId = this.defaultCategorizeId;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问答";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getData(id).then(response => {
        this.form = response.data;
        this.form.zhCategorizeName = this.zhDefaultCategorizeName;
        this.form.enCategorizeName = this.enDefaultCategorizeName;
        this.form.jaCategorizeName = this.jaDefaultCategorizeName;
        this.open = true;
        this.title = "修改问答";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$modal.confirm('是否确认该操作？').then(() => {
              updateData(this.form).then(response => {
                this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            })
          } else {
            this.$modal.confirm('是否确认该操作？').then(() => {
              addData(this.form).then(response => {
                this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除标题为"' + row.title + '"的数据项？').then(function() {
        return delData(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
